$(function () {
    // 初始化用户管理表
    $('#user').edatagrid({
        url: '/json/users.json',
        method: 'get',
        saveUrl: '',
        updateUrl: '',
        destroyUrl: '',
        destroyMsg: {
            norecord: {title: '警告', msg: '未选择任一行'},
            confirm: {title: '确认', msg: '是否删除？'}
        },
        idField: 'id',
        singleSelect: true,
        fitColumns: true,
        rownumbers: true,
        columns: [[
                {field: 'id', title: 'id', hidden: true, width: 100},
                {field: 'username', title: '用户名', width: 80, editor: 'text'},
                {field: 'passwd', title: '密码', width: 80, editor: 'text', formatter: function () {
                        return '******';
                    }},
                {field: 'phone', title: '电话', width: 130, editor: 'text'},
                {field: 'experience', title: '经验值', width: 80, editor: {type: 'numberbox', precision: 0}},
                {field: 'viplevel', title: '会员等级', width: 90, editor: {type: 'combobox', options: {
                            valueField: 'text', textField: 'text', data: [{text: '青铜会员'}, {text: '白银会员'}, {text: '黄金会员'}, {text: '钻石会员'}]
                        }}},
                {field: 'signature', title: '个性签名', width: 150}
            ]],
        pagination: true,
        pagePosition: 'both',
        border: false,
        fit: true
    });

    // 用户表分页
    var pager1 = $('#user').edatagrid('getPager');
    pager1.pagination({
        buttons: [{
                iconCls: 'icon-add',
                handler: function () {
                    $('#user').edatagrid('addRow');
                }
            }, {
                iconCls: 'icon-remove',
                handler: function () {
                    $('#user').edatagrid('destroyRow');
                }
            }, {
                iconCls: 'icon-undo',
                handler: function () {
                    $('#user').edatagrid('cancelRow');
                }
            }, {
                iconCls: 'icon-save',
                handler: function () {
                    $('#user').edatagrid('saveRow');
                }
            }
        ]
        , displayMsg: '共{total}条记录',
        showPageList: true
    });

    // 初始化省份城市列表

    $('#city').tree({
        url: '../json/city.json',
        method: 'get'
    });

    // 初始化站点表
    $('#route_table').edatagrid({
        url: 'route.json',
        method: 'get',
        saveUrl: '',
        updateUrl: '',
        destroyUrl: '',
        destroyMsg: {
            norecord: {title: '警告', msg: '未选择任一行'},
            confirm: {title: '确认', msg: '是否删除？'}
        },
        idField: 'id',
        singleSelect: true,
        fitColumns: true,
        rownumbers: true,
        columns: [[
                {field: 'id', title: 'id', hidden: true, width: 100},
                {field: 'name', title: '名称', width: 100, editor: 'text'},
                {field: 'start', title: '起点站', width: 150},
                {field: 'end', title: '终点站', width: 150}
            ]],
        pagination: true,
        pagePosition: 'top',
        border: false,
        fit: true
    });

    var pager2 = $('#route_table').edatagrid('getPager');
    pager2.pagination({
        layout: ['prev', 'next', 'refresh'],
        buttons: [{
                iconCls: 'icon-add',
                handler: function () {
                    $('#user').edatagrid('addRow');
                }
            }, {
                iconCls: 'icon-remove',
                handler: function () {
                    $('#user').edatagrid('destroyRow');
                }
            }, {
                iconCls: 'icon-undo',
                handler: function () {
                    $('#user').edatagrid('cancelRow');
                }
            }, {
                iconCls: 'icon-save',
                handler: function () {
                    $('#user').edatagrid('saveRow');
                }
            }
        ]
        , displayMsg: '共{total}条线路',
        showPageList: false
    });

    // 站点表
    $('#station_table').edatagrid({
        url: 'route.json',
        method: 'get',
        saveUrl: '',
        updateUrl: '',
        destroyUrl: '',
        destroyMsg: {
            norecord: {title: '警告', msg: '未选择任一行'},
            confirm: {title: '确认', msg: '是否删除？'}
        },
        idField: 'id',
        singleSelect: true,
        fitColumns: true,
        rownumbers: true,
        columns: [[
                {field: 'id', title: 'id', hidden: true, width: 100},
                {field: 'name', title: '名称', width: 100, editor: 'text'},
            ]],
        pagination: true,
        pagePosition: 'top',
        border: false,
        fit: true
    });

    var pager3 = $('#station_table').edatagrid('getPager');
    pager3.pagination({
        layout: ['prev', 'next', 'refresh'],
        buttons: [{
                iconCls: 'icon-add',
                handler: function () {
                    $('#user').edatagrid('addRow');
                }
            }, {
                iconCls: 'icon-remove',
                handler: function () {
                    $('#user').edatagrid('destroyRow');
                }
            }, {
                iconCls: 'icon-undo',
                handler: function () {
                    $('#user').edatagrid('cancelRow');
                }
            }, {
                iconCls: 'icon-save',
                handler: function () {
                    $('#user').edatagrid('saveRow');
                }
            }
        ]
        , displayMsg: '共{total}个站点',
        showPageList: false
    });
});


function collapseAll() {
    $('#city').tree('collapseAll');
}
function expandAll() {
    $('#city').tree('expandAll');
}